package cn.tedu.shoppingmall.controller;

import cn.tedu.shoppingmall.mapper.UserMapper;
import cn.tedu.shoppingmall.vo.User;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@Api(tags = "10用户模块--以弃用")
@RestController
public class AdminUserController {
    @Autowired(required = false)
    UserMapper mapper;

    //security接管了登录
    @RequestMapping("/login")
    public String login(HttpSession session, User user,String rem, HttpServletResponse response){ //rem用来确认记住密码
        User u = mapper.selectByUsername(user.getUsername());
        if (u!=null && "admin".equals(u.getUsername())) {
            if (u.getPassword().equals(user.getPassword())) {
                session.setAttribute("u", u);
                if (rem!=null){
                    //存cookie里
                    Cookie c1 = new Cookie("un",u.getUsername());
                    Cookie c2 = new Cookie("pw",u.getPassword());
                    //设置时间
                    c1.setMaxAge(60*60*24*30);
                    c2.setMaxAge(60*60*24*30);
                    //通过响应对象 将Cookie下发给客户端
                    response.addCookie(c1);
                    response.addCookie(c2);
//                    System.out.println();

                }
                return "ok"; //登录成功
            }
            return "密码错误";  //密码错误
        }
        return "您不是管理员";  //您不是管理员
    }

    //security接管退出登录
    @RequestMapping("/logOut")
    public void logout(HttpSession session){
        System.out.println("退出登录");
        session.removeAttribute("u");
    }
}
